﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace AppSettingsLib
{
    public static class XElementExtensions
    {
        public static XElement ToXElement<T>(this object obj)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (TextWriter streamWriter = new StreamWriter(memoryStream))
                {
                    var xmlSerializer = new XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(streamWriter, obj);
                    return XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray()));
                }
            }
        }

        public static T FromXElement<T>(this XElement xElement)
        {
            using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(xElement.ToString())))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));
                return (T)xmlSerializer.Deserialize(memoryStream);
            }
        }
    }
    public struct Config
    {
        public string webPage;
        public string seekString;
    }
    public class ConfigSet
    {
        public ConfigSet()
        {
            autoStart = false;
            config1.seekString = "";
            config1.webPage = "";
            config2.seekString = "";
            config2.webPage = "";
            config3.seekString = "";
            config3.webPage = "";
        }
        public bool autoStart;
        public Config config1;
        public Config config2;
        public Config config3;
    }

	public class AppSettings : AppSettingsBase
	{
		#region Data members
		// used to seed the base constructor
		private	const string APP_DATA_FOLDER	= "UsbVisitorsCounterConfig";
		private const string APP_DATA_FILENAME	= "Settings.xml";
		private const string FILE_COMMENT		= "User Seettings";
		// actual data
        protected ConfigSet configs = new ConfigSet();
		#endregion Data members

		#region Properties

		//--------------------------------------------------------------------------------
		/// <summary>
		/// Get/set age
		/// </summary>
        public ConfigSet Configs
		{
            get { return configs; }
            set { configs = value; }
		}
		//--------------------------------------------------------------------------------
		/// <summary>
		/// Get/set data as XElement object
		/// </summary>
        public override XElement XElement
        {
            get
            {
                return new XElement(SettingsKeyName, new XElement(Configs.ToXElement<ConfigSet>()));
            }
            set
            {
                IEnumerable<XElement> settings =
                    from el in value.Descendants("ConfigSet")
                    select el;
                XElement xe = settings.Single();
                Configs = xe.FromXElement<ConfigSet>();
            }
        }
		#endregion Properties

		#region Constructors
		//--------------------------------------------------------------------------------
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="isDefault"></param>
		/// <param name="value"></param>
		public AppSettings(string settingsKeyName, XElement element)
			: base(APP_DATA_FOLDER, APP_DATA_FILENAME, settingsKeyName, FILE_COMMENT, element)
		{
		}
		//--------------------------------------------------------------------------------
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="isDefault"></param>
		/// <param name="value"></param>
		public AppSettings(string appDataFolder, string appDataFilename, string settingsKeyName, string fileComment, XElement element)
			: base(appDataFolder, appDataFilename, settingsKeyName, fileComment, element)
		{
		}
		#endregion Constructors

	}
}
